/**   
 * 文件名：ReplyAction.java</br>
 * 描述： </br>
 * 开发人员：汪浩博 </br>
 * 创建时间： 2016-5-20
 */ 

package com.post.action;

import java.util.List;

import net.sf.json.JSONObject;

import com.base.BaseAction.BaseAction;
import com.base.util.Page;
import com.base.util.DateUtil;
import com.opensymphony.xwork2.ModelDriven;
import com.post.bean.Post;
import com.post.bean.Reply;
import com.post.bean.ReplyVO;
import com.post.service.PostService;
import com.post.service.ReplyService;
import com.user.bean.BBSUser;
import com.user.service.UserService;

/** 
 * 类名: ReplyAction</br> 
 * 包名：com.user.action </br> 
 * 描述: </br>
 * 发布版本号：</br>
 * 开发人员： 汪浩博</br>
 * 创建时间： 2016-5-20 
 */

public class ReplyAction extends BaseAction implements ModelDriven<Reply>{

	private Reply reply = new Reply();
	private ReplyService replyService;
	private UserService userService;
	private PostService postService;
	
	private String pid;			//帖子id
	private Integer pagenum = 1;
	private Integer normalPage = 1;
	
	/**
	 * 方法名: addReply</br>
	 * 详述: 新增回复</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-20</br>
	 * @return
	 */
	public String addReply(){
		String pid = reply.getPostId();
		Post post = postService.getPostById(pid);
		//刷新帖子回复数
		post.setReplyNumber(post.getReplyNumber()+1);
		//刷新帖子最后回复时间
		String lastreplytime = DateUtil.getStringTime();
		post.setLastreplytime(lastreplytime);
		postService.updatePost(post);
		
		reply.setId(null);
		reply.setReplyTime(DateUtil.getStringTime());
		replyService.addReply(reply);
		return "addReply";
	}
	
	/**
	 * 方法名: singlePost</br>
	 * 详述: 查看单个帖子以及回复</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-20</br>
	 * @return
	 */
	public String singlePost(){
		Post singlePost = replyService.getSinglePost(pid);
		singlePost.setViewNumber(singlePost.getViewNumber()+1);
		postService.addPost(singlePost);
		if(reply == null){
			reply = new Reply();
		}
		reply.setPostId(pid);
		//发帖用户
		BBSUser postUser = userService.findUserById(singlePost.getUserId());
		Page querypage = replyService.Querypage(reply, pagenum, pagesize);
		this.getRequest().setAttribute("pagenum", pagenum);
		this.getRequest().setAttribute("singlePost", singlePost);
		this.getRequest().setAttribute("postUser", postUser);
		
		//将结果集转换成VO
		ReplyVO replyVo = new ReplyVO();
		List queryResult = querypage.getQueryResult();
		List<ReplyVO> list = replyVo.beanTransVo(queryResult,userService);
		//将原有的结果集替换成VO结果集
		querypage.setQueryResult(list);
		this.getRequest().setAttribute("page", querypage);
		return "singlePost";
	}
	
	/**
	 * 方法名: personReply</br>
	 * 详述: 查询个人相关回复</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-20</br>
	 * @return
	 */
	public String personReply(){
		Long id = this.getLoginUser().getId();
		Page querypage = replyService.findPageReplyByUid(id,pagenum,pagesize);
		this.getRequest().setAttribute("page", querypage);
		return "personReply";
	}
	/**
	 * 方法名: personReply2</br>
	 * 详述: 个人中心里个人相关回复的帖子</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-11</br>
	 * @return
	 */
	public String personReply2(){
		JSONObject json = new JSONObject();
		String uid = this.getRequest().getParameter("uid");
		Page querypage = replyService.findPageReplyByUid(Long.parseLong(uid),pagenum,pagesize);
		json.put("page", querypage);
		this.writeJSON(json.toString());
		return null;
	}
	
	@Override
	public Reply getModel() {
		return this.reply;
	}

	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public ReplyService getReplyService() {
		return replyService;
	}
	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}

	public Integer getPagenum() {
		return pagenum;
	}

	public void setPagenum(Integer pagenum) {
		this.pagenum = pagenum;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public PostService getPostService() {
		return postService;
	}

	public void setPostService(PostService postService) {
		this.postService = postService;
	}

	public Integer getNormalPage() {
		return normalPage;
	}

	public void setNormalPage(Integer normalPage) {
		this.normalPage = normalPage;
	}
}
